package com.wyth.lottery.utils;


import com.sun.jna.Library;
import com.sun.jna.Native;
import com.sun.jna.ptr.IntByReference;

public class Vbar {

    private static final String ADDR = "127.0.0.1";
    private static final long PARM = 0;

    public interface Vdll extends Library {

        Vdll INSTANCE = (Vdll) Native.loadLibrary("vbar", Vdll.class);

        //打开设备
        public IntByReference vbar_open(String addr, long parm);

        //背光控制
        public boolean vbar_backlight(IntByReference vbar_device, boolean on);

        //蜂鸣器控制
        public boolean vbar_beep(IntByReference vbar_device, int times);

        //判断设备是否已连接
        public boolean vbar_is_connected(IntByReference vbar_device);

        //添加要支持的码制
        public boolean vbar_add_symbol_type(IntByReference vbar_device, int type);

        //扫码
        public boolean vbar_scan(IntByReference vbar_device, IntByReference symbol_type, byte[] result_buffer, IntByReference result_size);

        //关闭设备
        public void vbar_close(IntByReference vbar_device);


    }


    //初始化设备变量
    IntByReference device = null;
    byte[] result_buffer = new byte[1024];
    IntByReference symbol_type = new IntByReference(256);
    IntByReference result_size = new IntByReference(1024);

    //打开设备
    public boolean vbarOpen() {

        device = Vdll.INSTANCE.vbar_open(ADDR, PARM);
        if (device == null) {
            device = Vdll.INSTANCE.vbar_open(ADDR, PARM);
            return true;
        }else {

        }

        return true;
    }

    //蜂鸣器控制
    public boolean vbarBeep(int times) {
        if (Vdll.INSTANCE.vbar_beep(device, times)) {

            LogUtils.s("  拉阿拉啦  2222   ");

            return true;
        } else {
            return false;
        }
    }

    //背光控制
    public boolean vbarBacklight(boolean bool) {
        if (Vdll.INSTANCE.vbar_backlight(device, bool)) {
            return true;
        } else {
            return false;
        }
    }


    //判断设备是否已连接
    public boolean vbarIsConnected() {
        if (Vdll.INSTANCE.vbar_is_connected(device)) {
            return true;
        } else {
            return false;
        }
    }

    //添加要支持的码制
    // 条码符号类别ID定义
//			#define VBAR_SYM_NONE           0   /* 空类型, 用于清空 */
//			#define VBAR_SYM_QRCODE         1
//			#define VBAR_SYM_EAN8           2
//			#define VBAR_SYM_EAN13          3
//			#define VBAR_SYM_ISBN13         4
//			#define VBAR_SYM_CODE39         5
//			#define VBAR_SYM_CODE93         6
//			#define VBAR_SYM_CODE128        7
//			#define VBAR_SYM_DATABAR        8
//			#define VBAR_SYM_DATABAR_EXP    9
//			#define VBAR_SYM_PDF417         10
//			#define VBAR_SYM_DATAMATRIX     11
//			#define VBAR_SYM_ITF            12
//			#define VBAR_SYM_ISBN10         13
//			#define VBAR_SYM_UPCE           14
//			#define VBAR_SYM_UPCA           15
    public boolean vbarAddSymbolType(int symbol_type) {
        if (Vdll.INSTANCE.vbar_add_symbol_type(device, symbol_type)) {
            return true;

        } else {
            return false;
        }
    }

    //扫码
    public String vbarScan() {

        String decode = null;

        if (Vdll.INSTANCE.vbar_scan(device, symbol_type, result_buffer, result_size)) {
            decode = new String(result_buffer, 0, result_size.getValue());
            return decode;
        } else {
            return null;
        }
    }

    //关闭设备
    public void vbarClose() {
        if (device != null) {
            Vdll.INSTANCE.vbar_close(device);
            device = null;
        }
    }

}



	
	

